package liangtWorkSpace.singleDemo;

/**
 * @ClassName Mag03
 * @Description : 功能说明
 * @Author : LiangT
 * @Date : 2020/4/9 18:27
 */
public class Mag06 {
    private static volatile Mag06 INSTANCE;//volatile防止指令重复

    private Mag06() {
    }

    //    不枷锁，可能多个对象，非单例
    public static Mag06 getInstance() {
        if (INSTANCE == null) {
            synchronized (Mag06.class) {
                if (INSTANCE == null) {
                    try {
                        Thread.sleep(1);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }

            }
            INSTANCE = new Mag06();
        }
        return INSTANCE;
    }

    public void m() {
        System.out.println("m");
    }

    public static void main(String[] args) {
//        传统多线程简单实现
        //        for (int i = 0; i < 100; i++) {
//            new Thread(new Runnable() {
//                @Override
//                public void run() {
//                    ///
//                }
//            }).start();
//        }
        for (int i = 0; i < 100; i++) {
            new Thread(() -> {
                System.out.println(Mag06.getInstance().hashCode());
            }).start();
        }
    }
}
